package Service;

import models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * Created by 郑凯 on 2016/6/11.
 */
@Service
public class LoginAndRegisterImp implements LoginAndRegisterService{


    @Autowired
    private HttpSession session;

    public boolean login(String name, String password) {

        User user = (User)Dao.LoginAndRegister.login(name,password);
        if(user != null)
        {
            session.setAttribute("uid",user.getUid());
            session.setMaxInactiveInterval(60*60*24*30);
            return true;
        }
        else return false;

    }

    public boolean register(String name, String password, String email) {
        if(Dao.LoginAndRegister.getUserByEmail(email)) {
            return false;
        }
        if(Dao.LoginAndRegister.getUserByName(name)) return false;
        return Dao.LoginAndRegister.register(name,password,email);
    }
}
